package com.njlime.cms.controller.app;

import com.njlime.cms.entities.Information;
import com.njlime.cms.entities.InformationComment;
import com.njlime.cms.entities.InformationUp;
import com.njlime.cms.service.app.AppInformationMotionService;
import com.njlime.cms.service.app.AppInformationService;
import com.njlime.cms.service.app.AppInformationUpService;
import com.njlime.core.base.controller.BaseAppController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.web.AppWxInterceptor;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.app.AppMemberTrackService;
import com.njlime.member.service.app.AppPublishReportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 热点控制器
 * Created by Dean on 2017/10/10
 */

@Controller
@RequestMapping(value = "/app/information")
public class AppInformationController extends BaseAppController {
    @Resource
    private AppInformationService appInformationService;
    @Resource
    private AppInformationUpService appInformationUpService;
    @Resource
    private AppMemberTrackService appMemberTrackService;
    @Resource
    private AppPublishReportService appPublishReportService;
    @Resource
    private AppInformationMotionService appInformationMotionService;

    /**
     * 获取资讯列表
     *
     * @param pageBean
     * @param information
     * @param request
     * @return
     */
    @RequestMapping(value = "/infoList", method = RequestMethod.GET)
    @ResponseBody
    public String infoList(HttpServletRequest request, PageBean pageBean, Information information, Integer type) {
        type = type == null ? 1 : type;
        information.setPublish_status(GlobalValue.PublishStatus.PUBLISHED);
        Integer user_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        Integer root_unit_id = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ROOT_UNIT_ID);
        switch (type) {
            case 1: {
                return toJsonListData(appInformationService.list(pageBean, information, root_unit_id));
            }
            case 2: { //banner图列表
                return toJsonListData(appInformationService.bannerList(information, root_unit_id));
            }
            case 3: { //非banner图列表（含专题内容）
                return toJsonListData(appInformationService.infoList(pageBean, information, user_id, root_unit_id));
            }
            case 4: { //专题列表
                return toJsonListData(appInformationService.topicList(pageBean, information, root_unit_id));
            }
            default: {
                return toJsonListData(appInformationService.list(pageBean, information, root_unit_id));
            }
        }
    }

    /**
     * 获取资讯详细信息
     */
    @ResponseBody
    @RequestMapping(value = "/detail")
    public String detail(HttpServletRequest request, Integer id) {
        Integer unitId = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_UNIT_ID);
        Integer rootUnitId = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ROOT_UNIT_ID);
        Integer userId = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        if (userId != null) {
            appMemberTrackService.save(userId, GlobalValue.BusinessType.INFORMATION, id);
        }
        appInformationService.updateReading(id, null);
        Map<String,Object> detail = appInformationService.get(id, unitId, userId);

        //ConfigItem configItem = configService.getByName("info_month_charge", rootUnitId);
        //detail.put("month_charge",Double.valueOf(configItem.getConfig_value()));
        Integer business_unit = Integer.valueOf(detail.get("unit_id").toString());
        appPublishReportService.save(GlobalValue.BusinessType.INFORMATION, id, unitId, business_unit);
        return toJsonSuccessData(detail);
    }

    /**
     * 获取评论列表
     */
    @ResponseBody
    @RequestMapping(value = "/commentList")
    public String commentList(HttpServletRequest request, PageBean pageBean, InformationComment informationComment) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        return toJsonListData(appInformationService.commentList(pageBean, informationComment, uid));
    }

    /**
     * 添加评论
     *
     * @param request
     * @param informationComment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/commentMake")
    public String commentMake(HttpServletRequest request, InformationComment informationComment) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        String name = (String) request.getSession().getAttribute(AppWxInterceptor.WX_USER_NAME);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "请登录后添加评论");
            } else {
                informationComment.setUser_id(uid);
                informationComment.setUser_name(name);
                informationComment.setCreate_time(new Date());
                informationComment.setFabulous_num(0);
                informationComment.setCreator(uid);
                informationComment.setIs_del(GlobalValue.NO);
                informationComment.setIs_top(GlobalValue.NO);
                Information information = appInformationService.findOne(informationComment.getInformation_id());
                if(information.getIs_comment() == GlobalValue.YES){
                    informationComment.setStatus(GlobalValue.UNAUDIT);
                    appInformationService.addComment(informationComment, null);
                    return toJsonErrorData(ExceptionState.NEED_AUDIT_EXCEPTION,"添加成功，请等待审核");
                }else{
                    informationComment.setStatus(GlobalValue.AUDITED);
                    //用户评论
                    MemberComment memberComment = new MemberComment();
                    memberComment.setMember_id(informationComment.getUser_id());
                    memberComment.setBusiness_id(informationComment.getInformation_id());
                    memberComment.setBusiness_type(GlobalValue.BusinessType.INFORMATION);
                    memberComment.setBusiness_title(information.getTitle());
                    memberComment.setCreate_time(new Date());
                    appInformationService.addComment(informationComment, memberComment);
                    return toJsonSuccessData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "评论添加失败");
        }
    }

    /**
     * 点赞
     */
    @ResponseBody
    @RequestMapping(value = "upAdd")
    public String upAdd(HttpServletRequest request, InformationUp informationUp) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "用户未登录");
            } else {
                if (appInformationUpService.isUped(informationUp.getComment_id(), uid)) {
                    appInformationUpService.del(informationUp.getComment_id(), uid);
                    return toJsonSuccessData();
                } else {
                    informationUp.setUp_user_id(uid);
                    informationUp.setComment_id(informationUp.getComment_id());
                    informationUp.setInformation_id(informationUp.getInformation_id());
                    informationUp.setCreate_time(new Date());
                    appInformationUpService.save(informationUp);
                    return toJsonSuccessData();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "点赞操作失败");
        }
    }

    /**
     * 粉丝情绪
     * @param request
     * @param id
     * @param motion
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "motion")
    public String motion(HttpServletRequest request, Integer id, String motion) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        try {
            if (uid == null) {
                return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "用户未登录");
            } else {
                if(appInformationMotionService.getInformationMotion(id, uid) != null){
                    return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "已经发表过情绪了哦");
                }
                appInformationService.saveMotion(id, uid, motion);
                return toJsonSuccessData();
            }
        } catch (Exception e) {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "发表情绪失败");
        }
    }

    /**
     * 作者详情
     * @param request
     * @param unit_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "author")
    public String author(HttpServletRequest request,Integer unit_id) {
        Integer uid = (Integer) request.getSession().getAttribute(AppWxInterceptor.WX_USER_ID);
        return toJsonSuccessData(appInformationService.author(unit_id, uid));
    }

    /**
     * 作者列表
     * @param request
     * @param unit_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "authorList")
    public String authorList(HttpServletRequest request,PageBean pageBean, Integer unit_id) {
        return toJsonListData(appInformationService.authorList(pageBean, unit_id));
    }

}
